package com.uniledger.exchange.bank;

import android.app.Activity;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.uniledger.commonlib.util.DisplayUtil;
import com.uniledger.commonlib.view.FullListView;
import com.uniledger.exchange.R;
import com.uniledger.exchange.model.HttpResult;
import com.uniledger.exchange.model.api.Api;
import com.uniledger.exchange.model.rx.RxException;
import com.uniledger.exchange.model.rx.RxProgress;
import com.uniledger.exchange.model.rx.RxSchedulers;

import io.reactivex.functions.Consumer;
import me.shaohui.bottomdialog.BottomDialog;

public class BottomSelectDialog {

    private Activity activity;
    private FragmentManager manager;
    private BottomDialog dialog;
    private OnSelectBankListener selectBankListener;
    private FullListView recyclerView;
    private BaseAdapter adapter;

    public BottomSelectDialog(Activity activity, FragmentManager manager) {
        this.activity = activity;
        this.manager = manager;
    }

    public void setSelectBankListener(OnSelectBankListener selectBankListener) {
        this.selectBankListener = selectBankListener;
    }

    /**
     * 初始化对话框
     */
    public void init() {
        int height = DisplayUtil.getScreenHeight(activity);
        dialog = BottomDialog.create(manager)
                .setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(View v) {
                        bindDialogView(v);
                    }
                })
                .setLayoutRes(R.layout.fragment_bottom_bank_dialog)
                .setDimAmount(0.2f)
                .setCancelOutside(false)
                .setHeight(height / 2)
                .setTag("BottomBank")
        ;
    }

    public void show(BaseAdapter adapter) {
        if (dialog != null) {
            this.adapter = adapter;
            dialog.show();

        }
    }

    private void bindDialogView(View v) {
        recyclerView = (FullListView) v.findViewById(R.id.recycler_bank_bottom);
        recyclerView.setAdapter(adapter);



        recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object item = parent.getAdapter().getItem(position);
                if (item instanceof BankCard) {
                    BankCard card = (BankCard) item;
                    if (parent.getAdapter().getCount() - 1 == position) {
                        //TODO 处理添加银行卡操作
                        AddBankActivity.start(activity);
                    } else {
                        if (selectBankListener != null) {
                            selectBankListener.selectBank(card);
                        }
                        closeDialog();
                    }
                } else if (item instanceof Gateway) {
                    Gateway gateway = (Gateway) item;
                    if (selectBankListener != null) {
                        selectBankListener.selectGateway(gateway);
                    }
                    closeDialog();
                }
            }
        });

        TextView close = (TextView) v.findViewById(R.id.txt_dialog_close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeDialog();
            }
        });
    }

    public void closeDialog() {
        Fragment prev = manager.findFragmentByTag("BottomBank");
        if (prev != null) {
            DialogFragment df = (DialogFragment) prev;
            df.dismiss();
        }
    }

    public interface OnSelectBankListener {
        void selectBank(BankCard card);

        void selectGateway(Gateway gateway);
    }
}
